package com.database.service.impl;

import com.database.character.Comment;
import com.database.character.Post;
import com.database.mapper.CommentMapper;
import com.database.service.CommentService;
import com.database.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class CommentServiceImpl implements CommentService {

	@Autowired
	private CommentMapper commentMapper;
	@Autowired
	private PostService postService;
	@Override
	public int addComment(Comment comment) {
		comment.setPostTime(LocalDateTime.now());
		if(comment.getCommentType()==1){
			comment.setReplyId(null);
		}else comment.setReplyCommentId(null);
		System.out.println(comment);
		int i1 = commentMapper.addComment(comment);
		if(i1==0){
			return 0;
		}
		else{
			if(comment.getCommentType()==0) {
				Integer postId = comment.getReplyId();
				Post postByPid = postService.getPostByPid(postId);
				postByPid.setCommentNum(postByPid.getCommentNum() + 1);
				int i = postService.updatePost(postByPid);
				if (i == 0) {
					return 0;
				} else {
					return 1;
				}
			}
		}
		return 1;
	}

	@Override
	public List<Comment> getCommentByID(Integer replyId , Integer commentType) {
		if(commentType==0) return commentMapper.getByCommentType(replyId, commentType);
		else return commentMapper.getByCommentTypeElse(replyId, commentType);
	}

	@Override
	public int deleteComment(Integer postID) {
		return commentMapper.deleteCommentByArticleId(postID);
	}

	@Override
	public int deleteCommentByCommentId(Integer commentId) {
		return commentMapper.deleteCommentByCommentId(commentId);
	}

	@Override
	public Comment getCommentByCommentId(Integer commentId) {
		return commentMapper.getCommentByCommentId(commentId);
	}

	@Override
	public int updateCommentByCommentId(Comment comment) {
		comment.setPostTime(LocalDateTime.now());
		return commentMapper.updateCommentByCommentId(comment);
	}
}
